package com.explam.springboot.controller.admin;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.explam.springboot.common.ResultUtil;
import com.explam.springboot.entity.User;
import com.explam.springboot.mapper.PetalsMapper;
import com.explam.springboot.service.IUserService;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.List;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.explam.springboot.service.IPetalsService;
import com.explam.springboot.entity.Petals;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author 作者
 * @since 2024-04-17
 */
@RestController
@RequestMapping("/admin/petals")
public class PetalsController {

    @Resource
    private IPetalsService petalsService;

    @Resource
    private PetalsMapper petalsMapper;

    /*
    * 查询不是管理员或者不是父母的数据
    * */
    @GetMapping("/find-children")
    public ResultUtil getChildrenList(){
        List<User> userList = petalsMapper.getChildrenList();
        return ResultUtil.success(userList);
    }

    /*
     * 查询不是管理员或者不是父母的数据
     * */
    @GetMapping("/find-admin")
    public ResultUtil getAdminList(){
        List<User> adminList = petalsMapper.getAdminList();
        return ResultUtil.success(adminList);
    }

    //新增
    @PostMapping("/save")
    public ResultUtil save(@RequestBody Petals petals) {
        petalsService.save(petals);
        return ResultUtil.success("success");
    }

    //修改
    @PostMapping("/update")
    public ResultUtil edit(@RequestBody Petals petals) {
        petalsService.saveOrUpdate(petals);
        return ResultUtil.success("success");
    }

    //删除
    @GetMapping("/delete/id={id}")
    public ResultUtil delete(@PathVariable Integer id) {
        petalsService.removeById(id);
        return ResultUtil.success();
    }

    //批量删除
    @PostMapping("/del/batch")
    public ResultUtil deleteBatch(@RequestBody List<Integer> ids) {
        petalsService.removeByIds(ids);
        return ResultUtil.success();
    }

    //查询所有数据
    @GetMapping("/all")
    public ResultUtil findAll() {
        return ResultUtil.success(petalsService.list());
    }

    //根据id查询
    @GetMapping("/get-petals/id={id}")
    public ResultUtil findOne(@PathVariable Integer id) {
        return ResultUtil.success(petalsService.getById(id));
    }

    //分页查询
    @GetMapping("/page")
    public ResultUtil findPage(@RequestParam Integer pageNum,
                               @RequestParam Integer pageSize,
                               @RequestParam(defaultValue = "") String nickname,
                               @RequestParam(defaultValue = "") String propertyName) {
        QueryWrapper<Petals> queryWrapper = new QueryWrapper<>();
        if(StrUtil.isNotBlank(nickname)){
            queryWrapper.like("nickName", nickname);
        }
        if(StrUtil.isNotBlank(propertyName)){
            queryWrapper.like("propertyName", propertyName);
        }
        queryWrapper.orderByDesc("id");
        return ResultUtil.success(petalsService.page(new Page<>(pageNum, pageSize), queryWrapper));
    }
}

